﻿using Common;
using Common.XmlHandle;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace SFJDManagement
{
    /// <summary>
    /// LedgerWindow.xaml 的交互逻辑
    /// </summary>
    public partial class Win_Ledger : Window
    {
        private DateTime timeS;
        private DateTime timeE = DateTime.Now.Date;
        private DatePicker iTimeS;
        private DatePicker iTimeE;
        Window win_Parent;

        public Win_Ledger(Window window)
        {
            win_Parent = window;
            win_Parent.Visibility = Visibility.Hidden;
            Visibility = Visibility.Visible;
            InitializeComponent();
            Title = "查询";
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            win_Parent.Visibility = Visibility.Visible;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Thickness mThickness = new Thickness();
            mThickness.Left = 10;
            mThickness.Right = 10;
            mThickness.Top = 5;
            mThickness.Bottom = 5;
            Label label = new Label()
            {
                Content = "选择起始时间：",
                Margin = mThickness,
                FontSize = 18,
                HorizontalAlignment = HorizontalAlignment.Center
            };

            iTimeS = new DatePicker();
            iTimeS.Width = 150;
            iTimeS.Height = 30;
            iTimeS.FontSize = 18;
            iTimeS.DisplayDateStart = new DateTime(2017, 1, 1);
            iTimeS.DisplayDateEnd = timeE;
            iTimeS.SelectedDateChanged += PTimeS_SelectedDateChanged;
            iTimeS.Margin = mThickness;
            Label iLabel1 = new Label();
            iLabel1.FontSize = 18;
            iLabel1.VerticalAlignment = VerticalAlignment.Center;
            iLabel1.Content = "至";
            iTimeE = new DatePicker();
            iTimeE.Width = 150;
            iTimeE.Height = 30;
            iTimeE.FontSize = 18;
            iTimeE.DisplayDateEnd = timeE;
            iTimeE.SelectedDate = timeE;
            iTimeE.Margin = mThickness;
            //banner.Children.Add(iEdit);
            banner.Children.Add(label);
            banner.Children.Add(iTimeS);
            banner.Children.Add(iLabel1);
            banner.Children.Add(iTimeE);
            LoadDataGrid();
        }

        private void LoadDataGrid()
        {
            dataGrid.IsReadOnly = true;
            List<ClassData> liData = Runtime.xmlData.GetAllData();
            ConvertData covert = new ConvertData(Runtime.programPath);
            if (Runtime.config.UserLicense == License.User & !Runtime.config.IsAdmin)
            {

                List<ClassData> temp = new List<ClassData>(liData.Count);
                foreach (ClassData dat in liData)
                {
                    if(dat.User == Runtime.config.UserGuid)
                    {
                        temp.Add(dat);
                    }
                }
                dataGrid.ItemsSource = covert.DataList2Plus(temp);
            }
            else
            {
                dataGrid.ItemsSource = covert.DataList2Plus(liData);
            }

			dataGrid.AutoGenerateColumns = false;
			
			DataGridTextColumn c01 = new DataGridTextColumn();
			c01.Header = "字号";
			c01.Binding = new Binding("字号");
			dataGrid.Columns.Add(c01);
			DataGridTextColumn c02 = new DataGridTextColumn();
			c02.Header = "编码";
			c02.Binding = new Binding("编码");
			dataGrid.Columns.Add(c02);
			DataGridTextColumn c03 = new DataGridTextColumn();
			c03.Header = "市";
			c03.Binding = new Binding("市");
			dataGrid.Columns.Add(c03);
			DataGridTextColumn c04 = new DataGridTextColumn();
			c04.Header = "区县";
			c04.Binding = new Binding("区县");
			dataGrid.Columns.Add(c04);
			DataGridTextColumn c05 = new DataGridTextColumn();
			c05.Header = "大队";
			c05.Binding = new Binding("大队");
			dataGrid.Columns.Add(c05);
			DataGridTextColumn c06 = new DataGridTextColumn();
			c06.Header = "民警";
			c06.Binding = new Binding("民警");
			dataGrid.Columns.Add(c06);
			DataGridTextColumn c07 = new DataGridTextColumn();
			c07.Header = "类型";
			c07.Binding = new Binding("类型");
			dataGrid.Columns.Add(c07);
			DataGridTextColumn c08 = new DataGridTextColumn();
			c08.Header = "鉴定项目";
			c08.Binding = new Binding("鉴定项目");
			dataGrid.Columns.Add(c08);
			DataGridTextColumn c09 = new DataGridTextColumn();
			c09.Header = "初稿";
			c09.Binding = new Binding("初稿");
			dataGrid.Columns.Add(c09);
			DataGridTextColumn c10 = new DataGridTextColumn();
			c10.Header = "事故时间";
			c10.Binding = new Binding("发生时间") { StringFormat = "MM-dd" };
			dataGrid.Columns.Add(c10);
			DataGridTextColumn c11 = new DataGridTextColumn();
			c11.Header = "委托时间";
			c11.Binding = new Binding("委托时间") {StringFormat = "MM-dd" };
			dataGrid.Columns.Add(c11);
			DataGridTextColumn c12 = new DataGridTextColumn();
			c12.Header = "完稿时间";
			c12.Binding = new Binding("完稿时间") {StringFormat = "MM-dd" };
			dataGrid.Columns.Add(c12);
			DataGridTextColumn c13 = new DataGridTextColumn();
			c13.Header = "发送时间";
			c13.Binding = new Binding("发送时间") { StringFormat = "MM-dd" };
			dataGrid.Columns.Add(c13);
			DataGridTextColumn c14 = new DataGridTextColumn();
			c14.Header = "发送人";
			c14.Binding = new Binding("发送人");
			dataGrid.Columns.Add(c14);
			DataGridTextColumn c15 = new DataGridTextColumn();
			c15.Header = "收费情况";
			c15.Binding = new Binding("收费情况");
			dataGrid.Columns.Add(c15);
			DataGridTextColumn c16 = new DataGridTextColumn();
			c16.Header = "备注";
			c16.Binding = new Binding("备注");
			dataGrid.Columns.Add(c16);

            //IVehicleColumn c18 = new IVehicleColumn();
            IColumnVehicle c18 = new IColumnVehicle();
			c18.Header = "车辆及项目";
			c18.Binding = new Binding("车辆及项目");
            c18.IsReadOnly = true;
			dataGrid.Columns.Add(c18);
		}

        private void PTimeS_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            timeS = (DateTime)iTimeS.DisplayDate;
            iTimeE.DisplayDateStart = timeS;
        }

		public class VehicleList2StringConverter : IValueConverter
		{
			public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
			{
				string b = "";
				List<ClassVehicle> list = (List<ClassVehicle>)value;
				foreach(ClassVehicle veh in list)
				{
					b += veh.Number + ", ";
					b += veh.Brand + ", ";
					b += veh.Type + ", ";
					if (veh.Procj) { b += "车检、"; };
					if (veh.Procs) { b += "车速、"; };
					if (veh.Prohj) { b += "痕迹、"; };
					if (veh.Pross) { b += "车损、"; };
					if (veh.Procy) { b += "成因、"; };
					if (veh.Progc) { b += "过程、"; };
					if (veh.Prosx) { b += "属性、"; };
					if (b == null || b == "") { b = "未知、"; };
					b += "；";
				}
				return b.Substring(0, b.Length - 2);
				throw new NotImplementedException();
			}

			public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
			{
				throw new NotImplementedException();
			}
		}
	}
}
